$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'), 'ims/produce/getBrand',{});
            this.dataGrid.init().parseToolbar().activeButton(['bt_list_del'],['']);
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;

            //新增
            $('#bt_list_add').click(function () {
                $this.funObj().openBrandForm()
            });

            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
            }).click(function () {
                $this.funObj().askDelQuestion()
            });
            //搜索
            $('#bt_list_search').click(function () {
                $this.funObj().doSearch()
            });
        },
        funObj:function () {
            var $this = this;
            return {
                openBrandForm:function (row) {
                    window.top.swal({
                        titleText: row ? '修改品牌' : '新增品牌',
                        html:'<table style="margin-left: 30px;"><tr><td><label style="width: 75px;">名称</label></td><td><input type="text" class="form-control" id="name" style="display: inline;width: 300px;margin-bottom: 10px;"></td></tr>' +
                        '<tr><td><label style="width: 75px;">备注</label></td><td><textarea id="desc" name="desc"  class="form-control" rows="5" style="display: inline;width: 300px;"></textarea></td></tr></table>',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        onOpen:function () {
                            if (row) {
                                $('#name', window.top.document).val(row['sBrand']);
                                $('#desc', window.top.document).val(row['mDesc']);
                            }
                        },
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var name = $('#name', window.top.document).val();
                                if (!name) {
                                    reject('名称必填！');
                                } else {
                                    resolve(name);
                                }
                            })
                        }
                    }).then(function (name) {
                        var params = {sBrand:name};
                        params.mDesc = $('#desc', window.top.document).val();
                        if (row) {
                            params.id = row['iPBId'];
                        }
                        xhExtra.ajaxRequest('ims/produce/saveBrand', params, {}, function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.dataGrid.reload();
                        });
                    }, function(dismiss) {});
                },
                askDelQuestion:function () {
                    var ids = $this.dataGrid.getSelectValues('iPBId');
                    xhExtra.askChangeStatus('ims/produce/delBrand',{ids:ids},$this.dataGrid,'确定要删除吗',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                bindDgEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        $this.funObj().openBrandForm(rowData);
                    }
                },
                doSearch:function () {
                    $this.dataGrid.reload({
                        key:xhExtra.getValue($('#list_search'))
                    })
                }
            }
        }
    }
};